#!/bin/bash
#
#HELP:
if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo -e "Pouziti scriptu:\tsh build.sh [arg1 [arg2]]"
    echo arg1:
    echo -e "\thelp, -h, --help\t zobrazi tuto napovedu"
    echo -e "\t-r \t odstrani stare soubory"
    echo -e "\t-R \t pouze odstrani soubory, nebuilduje"
    echo arg2:
    echo -e "\t-b \t pri pouziti -r/-R odstrani i vygenerovanou bibliografii"
    exit
fi

#SETTINGS:
FILE="Trading space for time in explicit-state model checking"
BIBER=0
if [ $2 == "-b" ] || [ ! -f "$FILE.bbl" ]; then
    echo "Nenalezen soubor .bbl, bibliografie bude znovu generovana"
    BIBER=1
fi


#FUNCTIONS
function f_separator {
    local SEPARATOR
    for ((I=1; I <= $1; I++));
    do
        SEPARATOR=$SEPARATOR$2
    done
    echo $SEPARATOR
}

function f_echoNewBlock {
    echo -e "\n"
    f_separator 80 "="
    echo $1
    f_separator 40 "-"
}

function f_pdfLatex {
    f_echoNewBlock "Spoustim PDFLatex..."
    pdflatex "$FILE.tex" | grep -i -E 'warn|erro'
}

#START:
if [ "$1" == "-r" ] || [ "$1" == "-R" ]; then
    echo Priznak -r/-R. Mazu vygenerovane soubory...
    echo 
    if [ -e ./clrBuild.sh ]; then
        sh ./clrBuild.sh $2
        if [ "$1" == "-R" ]; then
            exit
        fi
    else
        echo Nenalezen soubor clrBuild.sh! Nelze smazat predchozi build
    fi
fi 
f_echoNewBlock "Spoustim Build!!"
f_pdfLatex
if [ $BIBER -gt 0 ]; then
    f_echoNewBlock "Spoustim biber..."
    biber "$FILE"
    f_pdfLatex
fi
(acroread "$FILE.pdf" >/dev/null 2>&1)&

echo hotovo!
